import axios, { AxiosInstance, AxiosError, InternalAxiosRequestConfig } from 'axios';
axios.defaults.withCredentials = true
const http: AxiosInstance = axios.create({
    baseURL: '/api',
    timeout: 50000
});

http.interceptors.request.use(
    (config: InternalAxiosRequestConfig) => {
        const cookie = localStorage.getItem("cookie") as string
        if (cookie) {
            config.headers!.Authorization = cookie;
        }
        return config;
    },
    (error: AxiosError) => {
        console.log(error);
        return Promise.reject();
    }
);

http.interceptors.response.use(
    (response: any) => {
        if (response.status === 200) {
            return response;
        } else {
            Promise.reject();
        }
    },
    (error: any) => {
        return Promise.reject(error);
    }
);

export default http;
